package com.mingtu.jgspatrol.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mingtu.common.view.MultiGridView;
import com.mingtu.jgspatrol.R;


public class HardwareLayerAdapter extends BaseAdapter {
    private Context context;
    private OnItemClickListener mOnItemClickListener = null;
    //    private String[] mData = {"红外相机", "卡口相机", "森林防火",
//            "制高点", "科研监测", "气象",
//            "水文"};
    private String[] mData = {"红外相机", "视频监控", "气象", "水文"};
    private int[] layer = {R.drawable.selector_checkbox_layer_hardware1,
            R.drawable.selector_checkbox_layer_hardware2,
            R.drawable.selector_checkbox_layer_hardware6,
            R.drawable.selector_checkbox_layer_hardware7};


    private MultiGridView multiGridView;

    public HardwareLayerAdapter(Context context, MultiGridView multiGridView) {
        this.context = context;
        this.multiGridView = multiGridView;
    }

    @Override
    public int getCount() {
        return mData.length;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = View.inflate(context, R.layout.item_layer_hardware, null);

            holder.layout = (LinearLayout) convertView.findViewById(R.id.layout);
            holder.checkBox = (CheckBox) convertView.findViewById(R.id.checkbox);
            holder.tvText = (TextView) convertView.findViewById(R.id.tv_text);
            holder.checkBox.setBackgroundResource(layer[position]);


//            ViewGroup.LayoutParams lp = holder.checkBox.getLayoutParams();
//            int screenWidth = ScreenUtils.getScreenWidth(); //一行5个，间隙是10dp
//            int mWidth = (screenWidth - SizeUtils.dp2px(10) * 6) / 5;
//            int mHeight = mWidth * SizeUtils.dp2px(66) / SizeUtils.dp2px(66);
//            lp.width = mWidth;
//            lp.height = mHeight;
//            holder.checkBox.setLayoutParams(lp);


            holder.checkBox.setTag("checkbox" + position);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        String text = mData[position];

        holder.tvText.setText(text);


        ViewHolder finalHolder = holder;
        holder.layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CheckBox checkBox = (CheckBox) multiGridView.findViewWithTag("checkbox" + position);
                boolean isChecked = checkBox.isChecked();
                if (mOnItemClickListener != null) {
                    //注意这里使用tag方法获取position
                    if (isChecked) {
                        finalHolder.checkBox.setChecked(false);
                        finalHolder.tvText.setSelected(false);
                    } else {
                        finalHolder.checkBox.setChecked(true);
                        finalHolder.tvText.setSelected(true);
                    }
                    mOnItemClickListener.onItemClick(v, position, !isChecked);
                }
            }
        });

        return convertView;
    }


    class ViewHolder {
        public LinearLayout layout;
        public CheckBox checkBox;
        public TextView tvText;
    }


    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    public static interface OnItemClickListener {
        void onItemClick(View view, int position, boolean isChecked);
    }

}
